from odoo import fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools.misc import format_date


class PurchaseSalesReportWizard(models.TransientModel):
    _name = 's.purchase.sales.summary.report.wizard'
    _description = 'Purchase and sales report'

    start_date = fields.Datetime('start date')
    end_date = fields.Datetime('end date')
    product_category_id = fields.Many2one('product.category', string='Product Category')

    def action_view(self):
        self.ensure_one()
        if self.start_date and self.end_date:
            start_date = min(self.start_date, self.end_date)
            end_date = max(self.start_date, self.end_date)
        else:
            start_date = self.start_date
            end_date = self.end_date
        return {
            'name': _('Purchase and sales report'),
            'view_mode': 'list',
            'res_model': 's.purchase.sales.summary.report',
            'type': 'ir.actions.act_window',
            'context': {
                'start_date': start_date,
                'end_date': end_date,
                'product_category_id': self.product_category_id.id,
            },
            'display_name': f'{format_date(self.env, start_date)} - {format_date(self.env, end_date)}',
        }
